package com.itheima.mall.service.smsService;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.PageBean;
import com.itheima.mall.commons.R;
import com.itheima.mall.mapper.smsMapper.SmsFlashPromotionMapper;
import com.itheima.mall.utils.TimeUtil;
import com.itheima.pojo.sms.SmsFlashPromotion;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


@DubboService
public class SmsFlashPromotionImpl extends ServiceImpl<SmsFlashPromotionMapper, SmsFlashPromotion> implements SmsFlashPromotionApi{

    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;
    /**
     * 添加
     */
    @Override
    public void saved(Map<String, Object> map) {
        //参数
        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        //秒杀时间段名称 creatTime
        String creatTime = ObjectUtil.toString(map.get("startDate"));
        smsFlashPromotion.setCreateTime(TimeUtil.getTimeForStrd(creatTime));
        //结束日期  endDate
        String endDate = ObjectUtil.toString(map.get("endDate"));
        smsFlashPromotion.setEndDate(TimeUtil.getTimeForStrd(endDate));
        //id
        Long id = (Long) map.get("id");
        smsFlashPromotion.setId(id);
        //开始日期  startDate
        String startDate = ObjectUtil.toString(map.get("startDate"));
        smsFlashPromotion.setCreateTime(TimeUtil.getTimeForStrd(startDate));
        //上下线状态  status
        Integer status = (Integer) map.get("status");
        smsFlashPromotion.setStatus(status);
        //title
        String title = (String) map.get("title");
        smsFlashPromotion.setTitle(title);
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }
    /**
     * 删除活动
     */
    @Override
    public void deleteFlash(Integer id) {
        smsFlashPromotionMapper.deleteById(id);
    }
    /**
     * 根据活动名称分页查询
     */
    @Override
    public PageBean<SmsFlashPromotion> listFlash(String keyword, Integer pageNum, Integer pageSize) {
        IPage<SmsFlashPromotion> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword);

        smsFlashPromotionMapper.selectPage(page,queryWrapper);

        PageBean<SmsFlashPromotion> pmsBrandPageBean = new PageBean<>(page);

        return pmsBrandPageBean;
    }
    /**
     * 修改上下线状态
     */
    @Override
    public R updateStatus(Integer id, Integer status) {
        return smsFlashPromotionMapper.updateStatus(status, id);
    }
    /**
     *  编辑活动
     */
    @Override
    public R updateFlashId(Integer id, SmsFlashPromotion smsFlashPromotion) {

        LambdaQueryWrapper<SmsFlashPromotion>queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SmsFlashPromotion::getId,id);

        smsFlashPromotionMapper.updateById(smsFlashPromotion);

        return R.ok(null);
    }
    /**
     *获取活动详情
     */
    @Override
    public R getFlash(Integer id) {
        LambdaQueryWrapper<SmsFlashPromotion>queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SmsFlashPromotion::getId,id);

        smsFlashPromotionMapper.selectById(id);

        return R.ok(null);
    }

}
